package wo.esys.test;

import java.util.Date;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import wo.common.util.WoUtil;
import wo.esys.mapper.MenuMapper;
import wo.esys.mapper.RoleMapper;
import wo.esys.mapper.UserMapper;
import wo.esys.po.Menu;
import wo.esys.po.Role;
import wo.esys.po.User;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ESysInit {

	@Resource // @Autowired
	private MenuMapper menuMapper;
	
	@Resource // @Autowired
	private RoleMapper roleMapper;
	
	@Resource // @Autowired
	private UserMapper userMapper;
	
	private final static String PWD = "123456";
	
	@Test
	@Rollback (false)
	@Transactional
	public void initData () {
		// 1.菜单
		// 1.1.上级菜单：系统管理
		Menu mEsys = new Menu ("esys", "系统管理", "00", null, null, null);
		menuMapper.create(mEsys);
		// 下级菜单：菜单
		Menu mMenu = new Menu ("esys-menu", "菜单", "0010", null, "esys/menu", mEsys);
		menuMapper.create(mMenu);
		// 下级菜单：用户
		Menu mUser = new Menu ("esys-user", "用户", "0020", null, "esys/user", mEsys);
		menuMapper.create(mUser);
		// 下级菜单：角色
		Menu mRole = new Menu ("esys-role", "角色", "0030", null, "esys/role", mEsys);
		menuMapper.create(mRole);
		// 下级菜单：数据字典
		Menu mDic = new Menu ("esys-dictionary", "数据字典", "0040", null, "esys/dictionary", mEsys);
		menuMapper.create(mDic);
		// 1.2.上级菜单：测试管理
		Menu mTest = new Menu ("test", "测试管理", "90", null, null, null);
		menuMapper.create(mTest);
		// 下级菜单：测试
		Menu mTest2 = new Menu ("test-test", "测试", "9010", null, "test/test", mTest);
		menuMapper.create(mTest2);
		// 2.角色
		// admin
		Role rAdmin = new Role ("admin", "admin", "系统管理员");
		roleMapper.create(rAdmin);
		// test
		Role rTest = new Role ("test", "test", "测试角色");
		roleMapper.create(rTest);
		// 3.角色/菜单关系数据
		// test角色，具有测试相关菜单
		roleMapper.relateMenu(rTest.getId(), mTest.getId());
		roleMapper.relateMenu(rTest.getId(), mTest2.getId());
		// 4.用户
		// admin
		User uAdmin = new User ("admin", "admin", WoUtil.getMD5("admin", PWD), new Date());
		userMapper.create(uAdmin);
		// test
		User uTest = new User ("test", "test", WoUtil.getMD5("test", PWD), new Date());
		userMapper.create(uTest);
		// 5.用户/角色关系数据
		// admin用户具有 admin角色
		userMapper.relateRole(uAdmin.getId(), rAdmin.getId());
		// test用户具有test角色
		userMapper.relateRole(uTest.getId(), rTest.getId());
	}
}
